package com.jaya.budan.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jaya.budan.R;
import com.jaya.budan.adapter.Fill_Information2_Adapter;
import com.jaya.budan.base.BaseActivity;
import com.jaya.budan.bean.User_Information;
import com.jaya.budan.utils.ToolUtils;
import com.jaya.budan.views.MyGridView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Fill_Information2_Activity extends BaseActivity implements View.OnClickListener {

    private Button btn_fill_informat2_next;
    private RelativeLayout back;
    private MyGridView gv_fill_item_type, gv_fill_item_juese, gv_fill_item_tixing, gv_fill_item_style;
    private TextView tv_fill_info2_juese, tv_fill_info2_tixing;
    private String nicename, height = "170cm", weight = "65kg", time_bron = "1990-01-01", access_token;
    private int sex = 1, int_type = 0, int_juese = 0, int_tixing = 0;
    private Map<Integer, String> list_style;
    private List<Integer> list_style2;
    private User_Information user_information;
    private Fill_Information2_Adapter fill_information2_adapter, fill_information2_adapter2, fill_information2_adapter3, fill_information2_adapter4;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fill_information2);
        Intent intent = getIntent();
        nicename = intent.getStringExtra("nicename");
        sex = intent.getIntExtra("sex", 1);
        time_bron = intent.getStringExtra("birthday");
        height = intent.getStringExtra("height");
        weight = intent.getStringExtra("weigh");
        access_token = intent.getStringExtra("access_token");
        user_information = (User_Information) intent.getSerializableExtra("user_information");
        back = findViewById(R.id.back);
        tv_fill_info2_juese = findViewById(R.id.tv_fill_info2_juese);
        tv_fill_info2_tixing = findViewById(R.id.tv_fill_info2_tixing);
        gv_fill_item_type = findViewById(R.id.gv_fill_item_type);
        gv_fill_item_juese = findViewById(R.id.gv_fill_item_juese);
        gv_fill_item_tixing = findViewById(R.id.gv_fill_item_tixing);
        gv_fill_item_style = findViewById(R.id.gv_fill_item_style);
        btn_fill_informat2_next = findViewById(R.id.btn_fill_informat2_next);
        list_style = new HashMap<>();
        btn_fill_informat2_next.setOnClickListener(this);
        back.setOnClickListener(this);
        gv_fill_item_type.setVerticalSpacing(15);
        gv_fill_item_type.setHorizontalSpacing(15);
        gv_fill_item_juese.setVerticalSpacing(15);
        gv_fill_item_juese.setHorizontalSpacing(15);
        gv_fill_item_tixing.setVerticalSpacing(15);
        gv_fill_item_tixing.setHorizontalSpacing(15);
        gv_fill_item_style.setVerticalSpacing(15);
        gv_fill_item_style.setHorizontalSpacing(15);
        if (user_information != null){
            fill_information2_adapter = new Fill_Information2_Adapter(this, user_information, 1);
            fill_information2_adapter2 = new Fill_Information2_Adapter(this, user_information, 2);
            fill_information2_adapter3 = new Fill_Information2_Adapter(this, user_information, 3);
            fill_information2_adapter4 = new Fill_Information2_Adapter(this, user_information, 4);
            gv_fill_item_type.setAdapter(fill_information2_adapter);
            gv_fill_item_juese.setAdapter(fill_information2_adapter2);
            gv_fill_item_tixing.setAdapter(fill_information2_adapter3);
            gv_fill_item_style.setAdapter(fill_information2_adapter4);
            gv_fill_item_type.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                ToolUtils.showToast(Fill_Information2_Activity.this, user_information.getType().get(position).getName());
                    fill_information2_adapter.setSelect(position);
                    fill_information2_adapter.notifyDataSetChanged();
                    if (user_information.getType().get(position).getName().equals("弯男") ||
                            user_information.getType().get(position).getName().equals("弯女") ||
                            user_information.getType().get(position).getName().equals("双性恋") ){
                        tv_fill_info2_juese.setVisibility(View.VISIBLE);
                        tv_fill_info2_tixing.setVisibility(View.VISIBLE);
                        gv_fill_item_juese.setVisibility(View.VISIBLE);
                        gv_fill_item_tixing.setVisibility(View.VISIBLE);
                        if (sex == 2){
                            tv_fill_info2_tixing.setText("形象");
                        }
                    } else {
                        tv_fill_info2_juese.setVisibility(View.GONE);
                        tv_fill_info2_tixing.setVisibility(View.GONE);
                        gv_fill_item_juese.setVisibility(View.GONE);
                        gv_fill_item_tixing.setVisibility(View.GONE);
                    }
                    int_type = user_information.getType().get(position).getValue();
                    if (int_type != 0){
                        btn_fill_informat2_next.setEnabled(true);
                    }
                }
            });
            gv_fill_item_juese.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                ToolUtils.showToast(Fill_Information2_Activity.this, user_information.getRole().get(position).getName());
                    fill_information2_adapter2.setSelect(position);
                    fill_information2_adapter2.notifyDataSetChanged();
                    int_juese = user_information.getRole().get(position).getValue();
                }
            });
            gv_fill_item_tixing.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                ToolUtils.showToast(Fill_Information2_Activity.this, user_information.getFace().get(position).getName());
                    fill_information2_adapter3.setSelect(position);
                    fill_information2_adapter3.notifyDataSetChanged();
                    int_tixing = user_information.getFace().get(position).getValue();
                }
            });
            gv_fill_item_style.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    RelativeLayout rl_fill_gv_item = view.findViewById(R.id.rl_fill_gv_item);
                    // 选中的值
                    Integer key = user_information.getStyle().get(position).getValue();
                    String value = list_style.get(key);
                    if(ToolUtils.isEmpty(value)){ // 没选中
//                    ToolUtils.showToast(Fill_Information2_Activity.this, user_information.getStyle().get(position).getName());
                        rl_fill_gv_item.setBackgroundResource(R.drawable.login_login);
                        list_style.put(key,"1");
                    } else {
                        list_style.remove(key);
                        rl_fill_gv_item.setBackgroundResource(R.drawable.login_exit);
//                    ToolUtils.showToast(Fill_Information2_Activity.this, user_information.getStyle().get(position).getName() + "-取消");
                    }
                }
            });
        }
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_fill_informat2_next:
                list_style2 = new ArrayList<>();
                for(Map.Entry<Integer, String> entry : list_style.entrySet()){
                    list_style2.add(entry.getKey());
                }
                Log.d("fill","list_style-------" + list_style2.toString());
                Intent intent = new Intent(this, Fill_Information3_Activity.class);
                intent.putExtra("nicename", nicename);
                intent.putExtra("sex", sex);
                intent.putExtra("birthday", time_bron);
                intent.putExtra("height", height);
                intent.putExtra("weigh", weight);
                intent.putExtra("access_token", access_token);
                intent.putExtra("int_type", int_type);
                intent.putExtra("int_juese", int_juese);
                intent.putExtra("int_tixing", int_tixing);
                intent.putExtra("list_style2", (Serializable) list_style2);
                intent.putExtra("user_information", user_information);
                startActivity(intent);
                break;
            case R.id.back:
                finish();
                break;
            default:
                break;
        }
    }
}
